Ignore kml, Document, Folder tags to improve read compaitiblity for KMLv2.
authorrobertl <robertl@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Wed, 19 Oct 2005 15:18:05 +0000 (15:18 +0000)
committerrobertl <robertl@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Wed, 19 Oct 2005 15:18:05 +0000 (15:18 +0000)
Also read MultiGeometry/LineStrings as tags.

gpsbabel/kml.c
gpsbabel/xmlgeneric.c

index 55d7ddeb34ff71552e25766129a6da0649b9c753..bce4b5c00f8f7fa9b7f21656c21045770db36db7 100644 (file)
@@ -35,6 +35,7 @@ static int export_points;
 static int floating;
 
 static waypoint *wpt_tmp;
+static int wpt_tmp_queued;
 
 static FILE *fd;
 static FILE *ofd;
@@ -85,27 +86,39 @@ kml_read(void)
 #else
 
 static xg_callback     wpt_s, wpt_e;
-static xg_callback     wpt_name, wpt_desc, wpt_coord;
+static xg_callback     wpt_name, wpt_desc, wpt_coord, trk_coord;
 
 static 
 xg_tag_mapping kml_map[] = {
-       { wpt_s,        cb_start,       "/Document/Folder/Placemark" },
-       { wpt_e,        cb_end,         "/Document/Folder/Placemark" },
-       { wpt_name,     cb_cdata,       "/Document/Folder/Placemark/name" },
-       { wpt_desc,     cb_cdata,       "/Document/Folder/Placemark/description" },
-       { wpt_coord,    cb_cdata,       "/Document/Folder/Placemark/Point/coordinates" },
+       { wpt_s,        cb_start,       "/Placemark" },
+       { wpt_e,        cb_end,         "/Placemark" },
+       { wpt_name,     cb_cdata,       "/Placemark/name" },
+       { wpt_desc,     cb_cdata,       "/Placemark/description" },
+       { wpt_coord,    cb_cdata,       "/Placemark/Point/coordinates" },
+       { trk_coord,    cb_cdata,       "/Placemark/MultiGeometry/LineString/coordinates" },
        { NULL,         0,              NULL }
 };
 
+static 
+const char * kml_tags_to_ignore[] = {
+       "kml",
+       "Document",
+       "Folder",
+       NULL,
+};
+
 void wpt_s(const char *args, const char **unused) 
 { 
        wpt_tmp = waypt_new();
-//     wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1);
+       wpt_tmp_queued = 0;
 }
 
 void wpt_e(const char *args, const char **unused)
 {
-       waypt_add(wpt_tmp);
+       if (wpt_tmp_queued) {
+               waypt_add(wpt_tmp);
+       }
+       wpt_tmp_queued = 0;
 }
 
 #if 0
@@ -134,6 +147,28 @@ void wpt_desc(const char *args, const char **unused)
 void wpt_coord(const char *args, const char **attrv)
 {
        sscanf(args, "%lf,%lf,%lf", &wpt_tmp->longitude, &wpt_tmp->latitude, &wpt_tmp->altitude);
+       wpt_tmp_queued = 1;
+}
+
+void trk_coord(const char *args, const char **attrv)
+{
+       int consumed = 0;
+       double lat, lon, alt;
+       waypoint *trkpt;
+
+       route_head *trk_head = route_head_alloc();
+       track_add_head(trk_head);
+       
+       while (3 == sscanf(args,"%lf,%lf,%lf %n", &lon, &lat, &alt, &consumed)){
+               trkpt = waypt_new();    
+               trkpt->latitude = lat;
+               trkpt->longitude = lon;
+               trkpt->altitude = alt;
+
+               route_add_wpt(trk_head, trkpt);
+
+               args += consumed;
+       }
 }
 
 static 
@@ -141,6 +176,7 @@ void
 kml_rd_init(const char *fname)
 {
        xml_init(fname, kml_map, NULL);
+       xml_ignore_tags(kml_tags_to_ignore);
 }
 
 static
index d23db8d314bf74eab8793d339ad178ae6e9c4d6b..d8895634e30c00237ff536f8705d4d94ea08a1be 100644 (file)
@@ -32,6 +32,7 @@ static vmem_t current_tag;
 static vmem_t cdatastr;
 static FILE *ifd;
 static xg_tag_mapping *xg_tag_tbl;
+static const char **xg_ignore_taglist;
 
 #define MY_CBUF 4096
 
@@ -155,6 +156,27 @@ xml_tbl_lookup(const char *tag, xg_cb_type cb_type)
        return NULL;
 }
 
+/*
+ * See if tag element 't' is in our list of things to ignore.
+ * Returns 0 if it is not on the list.
+ */
+static int
+xml_consider_ignoring(const char *t)
+{
+       const char **il;
+
+       if (!xg_ignore_taglist) {
+               return 0;
+       }
+
+       for (il = xg_ignore_taglist; *il; il++) {
+               if (0 == strcmp(*il, t)) {
+                       return 1;
+               }
+       }
+       return 0;
+}
+
 
 static void
 xml_start(void *data, const char *el, const char **attr)
@@ -163,6 +185,9 @@ xml_start(void *data, const char *el, const char **attr)
        char *ep;
        xg_callback *cb;
 
+       if (xml_consider_ignoring(el))
+               return;
+
        vmem_realloc(&current_tag, strlen(current_tag.mem) + 2 + strlen(el));
 
        e = current_tag.mem;
@@ -195,6 +220,9 @@ xml_end(void *data, const char *el)
        char *s = strrchr(current_tag.mem, '/');
        xg_callback *cb;
 
+       if (xml_consider_ignoring(el))
+               return;
+
        if (strcmp(s + 1, el)) {
                fprintf(stderr, "Mismatched tag %s\n", el);
        }
@@ -237,6 +265,11 @@ void xml_readstring( char *str )
        XML_ParserFree(psr);
 }
 
+void xml_ignore_tags(const char **taglist)
+{
+       xg_ignore_taglist = taglist;
+}
+
 void
 xml_init(const char *fname, xg_tag_mapping *tbl, const char *encoding)
 {
@@ -271,6 +304,7 @@ xml_deinit(void)
                fclose(ifd);
                ifd = NULL;
        }
+       xg_ignore_taglist = NULL;
 }
 
 /******************************************/